<?php
defined('BASEPATH') OR exit('No direct script access allowed');
	class Product_model extends CI_Model{
        public $table_name='product';
    	public function get_all(){
            $query=$this->db->get($this->table_name);
            return $query->result();
        }
        public function get_one($id){
            $query=$this->db->where('id',$id)->get($this->table_name);
            return $query->row();
        }
        public function get_last($data){
            $query=$this->db->limit($data)->get($this->table_name);
            return $query->result();
        }
        public function get_recommend($catalog_id){
        	$query=$this->db->where(array('catalog_id'=>$catalog_id,'recommend'=>1))->limit(1)->get($this->table_name);
            return $query->row();
        }
        public function get_no_recommend($catalog_id){
        	$query=$this->db->where(array('catalog_id'=>$catalog_id,'recommend'=>0))->limit(6)->get($this->table_name);
            return $query->result();
        }
        ///////获取当前分类及子分类
        public function get_child_id_img($catalog_id,$brand_id){
            $query=$this->db->where('parent_id',$catalog_id)->get('product_catalog');
            $child_catalogs=$query->result();
            $where=array($catalog_id);
            foreach($child_catalogs as $c){
                $where[]=$c->id;
            }
            $w=array();
            if($brand_id!=false){
                $w['brand_id']=$brand_id;
            }
            $query=$this->db->where_in('catalog_id',$where)->where($w)->get($this->table_name);
            return $query->result();
        }
        public function get_brand_by_id($catalog_id){
            $query=$this->db->where('parent_id',$catalog_id)->get('product_catalog');
            $child_catalogs=$query->result();
            $where=array($catalog_id);
            foreach($child_catalogs as $c){
                $where[]=$c->id;
            }
            $query=$this->db->select('brand_id')->where_in('catalog_id',$where)->distinct()->get($this->table_name);
            $brand_ids= $query->result();
            $brands=array();
            foreach ($brand_ids as $b){
               $brands[]=$b->brand_id;
            }
            $query=$this->db->where_in('id',$brands)->get('product_brand');
            return $query->result();
        }
        public function get_all_cart($where){
            $query=$this->db->where_in('id',$where)->get($this->table_name);
            return $query->result();
        }
        public function get_one_product($id){
            $query=$this->db->where('id',$id)->get($this->table_name);
            return $query->row();
        }


        // public function get_child_id_img($catalog_id,$brand_id){
        //     $w=array();
        //     if($brand_id!=false){
        //         $w['brand_id']=$brand_id;
        //     }
        //     $query=$this->db->where_in('catalog_id',$catalog_id)->where($w)->get($this->table_name);
        //     return $query->result();
        // }
        // public function get_brand_by_id($catalog_id,$brand_id){
        //     $query=$this->db->select('brand_id')->where_in('catalog_id',$catalog_id)->distinct()->get($this->table_name);
        //     $brand_ids= $query->result();
        //     $brands=array();
        //     foreach ($brand_ids as $b) {
        //        $brands[]=$b->brand_id;
        //     }
        //     $query=$this->db->where_in('id',$brands)->get('product_brand');
        //     return $query->result();
        // }
       
	}